package com.zzy.util.linuxutils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StartSh {

    public void startLinuxSh(){
        try {
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
            String date = sdf2.format(new Date());
            String shellComand = "sh /data/moveReportFile.sh >> /data/mmm/logs/moveReportFile." + date + ".log";
            //Runtime.getRuntime().exec(shellComand); // 次方式适合执行简单脚本，带 > 的的不能执行
            ProcessBuilder pb = new ProcessBuilder("bash", "-c", shellComand);
            Process p = pb.start();
            p.waitFor();

        } catch (Exception e) {
           e.printStackTrace();
        }
    }


    public static void startWindowsbat(){
        try {
            // 执行Windows命令
            Process process = Runtime.getRuntime().exec("cmd /c dir");
            // 读取命令执行结果
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            // 等待命令执行完成
            process.waitFor();

            // 关闭流
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        startWindowsbat();
    }
}
